package cn.exrick.xboot.modules.assessment.serviceimpl;

import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.assessment.dao.HkFormIndexDao;
import cn.exrick.xboot.modules.assessment.dao.HkFormRelationShipDao;
import cn.exrick.xboot.modules.assessment.entity.HkFormIndex;
import cn.exrick.xboot.modules.assessment.entity.HkFormRelationShip;
import cn.exrick.xboot.modules.assessment.service.HkFormIndexService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



/**
 * 表单项接口实现
 * @author yck
 */
@Slf4j
@Service
@Transactional
public class HkFormIndexServiceImpl implements HkFormIndexService {

    @Autowired
    private HkFormIndexDao hkFormIndexDao;
    @Autowired
    private HkFormRelationShipDao hkFormRelationShipDao;

    @Override
    public HkFormIndexDao getRepository() {
        return hkFormIndexDao;
    }

    @Override
    public Page<HkFormIndex> findByCondition(HkFormIndex hkFormIndex, SearchVo searchVo, Pageable pageable) {

        return hkFormIndexDao.findAll(new Specification<HkFormIndex>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<HkFormIndex> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");

                List<Predicate> list = new ArrayList<Predicate>();

                Path<String> formIndexName = root.get("formIndexName");
                if( StrUtil.isNotBlank(hkFormIndex.getFormIndexName())){
                    list.add(cb.like(formIndexName,'%'+hkFormIndex.getFormIndexName()+'%'));
                }


                // 创建时间
                if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }
    @Override
    @Transactional
    public void setCustomDeleteFormIndex(String id) throws Exception {
        List<HkFormRelationShip> allByItemId = hkFormRelationShipDao.findAllByItemId(id);
        if(allByItemId.size()>0){
            throw new Exception("该表单项已经关联表单，取消后再设置！");
        }
        hkFormIndexDao.setCustomDeleteFormIndex();
        HkFormIndex one = hkFormIndexDao.getOne(id);
        if(one.getIsCustomeAdd().equals("1")){
            throw new Exception("该表单项为自定义加分表单项不能同时设置为自定义减分项！");
        }
        one.setIsCustomeDelete("1");
        hkFormIndexDao.saveAndFlush(one);
    }

    @Override
    public void setCustomAddFormIndex(String id) throws Exception {
        List<HkFormRelationShip> allByItemId = hkFormRelationShipDao.findAllByItemId(id);
        if(allByItemId.size()>0){
            throw new Exception("该表单项已经关联表单，取消后再设置！");
        }
        hkFormIndexDao.setCustomAddFormIndex();
        HkFormIndex one = hkFormIndexDao.getOne(id);
        if(one.getIsCustomeDelete().equals("1")){
            throw new Exception("该表单项为自定义减分表单项不能同时设置为自定义加分项！");
        }
        one.setIsCustomeAdd("1");
        hkFormIndexDao.saveAndFlush(one);
    }

    @Override
    public List<HkFormIndex> getAllList(HkFormIndex hkFormIndex) {
        return hkFormIndexDao.getAll(hkFormIndex.getFormIndexName());
    }

}